﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using mvcForum.Core.DependencyBuilders;
using mvcForum.SearchProvider.Lucene;
using mvcForum.Web.DependencyBuilders;
using mvcForum.WebUI.Intrastructure;
using System.Configuration;

//using MvcMiniProfiler;

namespace mvcForum.WebUI {

	public class MvcApplication : HttpApplication {

		public override void Init() {
			base.Init();
		}

		//protected void Application_BeginRequest() {
		//    if (Request.IsLocal) {
		//        MiniProfilerEF.Initialize();
		//    }
		//}

		protected void Application_Start() {
			// If you need to replace the MVCForum standard binding with your own, do it here!
			// You should NOT remove the CoreBuilder or the WebCommonBuilder unless you know what you're doing!
			//new ApplicationInitializer(
			//                            new DependencyContainer(),
			//                            new IDependencyBuilder[] {
			//                                // TODO: Create = true, database created, use this on regular SQL Server
			//                                new mvcForum.DataProvider.EntityFramework.DataProviderConfiguration(true),
			//                                // TODO: Create = false, need a database, use this on Azure SQL
			//                                //new mvcForum.DataProvider.EntityFramework.DataProviderConfiguration(false),
			//                                new ProbeAssemblyBuilder(typeof(mvcForum.Web.ForumConfigurator).Assembly),
			//                                new CoreBuilder(),
			//                                new LuceneSearchBuilder(),
			//                                //new mvcForum.AmazonWebServices.AWSS3SearchBuilder(),
			//                                //new mvcForum.AmazonWebServices.AWSS3StorageBuilder(),
			//                                //new mvcForum.Azure.AzureSearchBuilder(),
			//                                new SampleSiteBuilder(),
			//                                new ProbeAssemblyBuilder(typeof(mvcForum.AddOns.AddOnBuilder).Assembly)
			//                        });

			// OR

			// This will do the bindings described in the web.config file!
			mvcForum.Web.ApplicationConfiguration.Initialize();

			// TODO: For performance and debugging! Remove on live site!
			//MiniProfilerEF.Initialize();
			// This is just the usual code needed for a MVC 3 application, exchange this with your own.
			AreaRegistration.RegisterAllAreas();
			RegisterRoutes(RouteTable.Routes);
			//RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
			GlobalFilters.Filters.Add(new HandleErrorAttribute());
		}

		public static void RegisterRoutes(RouteCollection routes) {
			//routes.RouteExistingFiles = false;
			// And again the standard code for a MVC 3 application, replace all this with your own!
			routes.IgnoreRoute("{file}.txt");
			routes.IgnoreRoute("{file}.htm");
			routes.IgnoreRoute("{file}.html");

			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" });

			routes.IgnoreRoute("content/{*pathInfo}");

			//routes.Add(new Route("tapatalk/mobiquo.mvc", new GetTopicRouteHandler()));

			routes.MapRoute(
				"Default", // Route name
				"{controller}/{action}/{id}", // URL with parameters
				new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
				// Remember this line (or a similar one) to avoid conflicts between the Home controller of the MVCForum areas
				// and your own!
				new string[] { "mvcForum.WebUI.Controllers" }
			);

			// Needed for OpenAuth, note, this is not part of mvcForum, for now this is just a way of authenticating.
			// mvcForum can handle Local and External user types, so there is no reason why you can't use DotNetOpenAuth with
			// mvcForum, it's just not "in the box", and you're on your own.
			//routes.MapRoute("OpenIdDiscover", "Auth/Discover");
		}
	}
}
